package com.swjd.avl;

/**
 * 平衡二叉树
 * @author 刘涛
 * @create 2021/10/6 16:47
 */
public class AVLTreeDemo {
    public static void main(String[] args) {
        int arr[] = {6, 7, 8, 9, 10, 11};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node(arr[i]));
        }
        //中序遍历
        avlTree.infixOrder();
        //左旋转平衡处理之前
        System.out.println(avlTree.getRoot().height());
        System.out.println(avlTree.getRoot().leftHeight());
        System.out.println(avlTree.getRoot().rightHeight());
        //中序遍历
        avlTree.infixOrder();
    }
}

/**
 * AVL树
 */
class AVLTree {
    private Node root;

    /**
     * 添加节点
     *
     * @param node
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("树空");
        }
    }

    /**
     * 删除节点
     *
     * @param value 要删除的值
     */
    public void delete(int value) {
        if (value == root.value && root.right != null) {
            Node node = root.right;
            while (true) {
                if (node.left == null) {
                    break;
                }
                node = node.left;
            }
            node.left = root.left;
            root = root.right;
        } else if (value == root.value && root.left != null && root.right == null) {
            root = root.left;
        } else if (root.right == null && root.left == null) {
            root = null;
        } else {
            root.delete(value);
        }
    }

    public Node getRoot() {
        return root;
    }
}


/**
 * 节点
 */
class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    /**
     * 添加节点
     *
     * @param node 传入的节点信息
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                //左子节点为空
                this.left = node;
            } else {
                this.left.add(node);
            }
        }
        if (node.value >= this.value) {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
//        添加完一个节点后，判断是否要转成平衡树（右子树高度比左子树高度大于一）
        if ((this.rightHeight() - this.leftHeight()) > 1) {
            //如果当前节点的右子树的左节点的高度,大于右子树的右节点的高度(即子树非平衡，先将子树平衡)
            if (this.right.leftHeight() > this.right.rightHeight()) {
                this.right.rightHeight();
            }
            this.leftRotate();
        }
        if ((this.leftHeight() - this.rightHeight()) > 1) {
            //如果当前节点的左子树的右子树的高度，大于左子树的左子树的高度
            if (this.left.rightHeight() > this.left.leftHeight()) {
                //先对当前节点的左子树进行左旋
                left.leftRotate();
            }
            rightRotate();
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 删除节点
     */
    public void delete(int value) {
        //如果右子节点匹配到了
        if (this.right != null && this.right.value == value) {
            if (this.right.left != null && this.right.right == null) {
                this.right = this.right.left;
                return;
            } else {
                Node node = this.right.right;
                while (node != null) {
                    if (node == null || node.left == null) {
                        break;
                    }
                    node = node.left;
                }
                if (node != null) {
                    node.left = this.right.left;
                }
                //左节点为空时，不管右节点是否为空，都可以把右节点提上去,当左节点不为空时，右节点不为空才能将右节点提上去
                this.right = this.right.right;
                return;
            }
        }
        //如果左子节点匹配到了
        if (this.left != null && this.left.value == value) {
            if (this.left.left != null && this.left.right == null) {
                this.left = this.left.left;
                return;
            } else {
                Node node = this.left.right;
                while (node != null) {
                    if (node == null || node.left == null) {
                        break;
                    }
                    node = node.left;
                }
                if (node != null) {
                    node.left = this.left.left;
                }
                //左节点为空时，不管右节点是否为空，都可以把右节点提上去,当左节点不为空时，右节点不为空才能将右节点提上去
                this.left = this.left.right;
                return;
            }
        }
        if (this.right != null) {
            this.right.delete(value);
        }
        if (this.left != null) {
            this.left.delete(value);
        }
    }

    /**
     * 返回左子树高度
     *
     * @return 高度
     */
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    /**
     * 返回右子树高度
     *
     * @return 高度
     */
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    /**
     * 树最大高度
     *
     * @return 高度
     */
    static int num = 0;

    public int height() {
        //递归返回最大的左右子树层数 即当前节点最大的树高度
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    /**
     * 左旋转实现平衡树
     */
    private void leftRotate() {
        //创建一个新的节点，以当前根节点作为值
        Node newNode = new Node(this.value);
        //将新的节点的左子树设置为当前节点的左子树
        newNode.left = this.left;
        //将新节点的右子树设置成当前节点的右子树的左子树
        newNode.right = this.right.left;
        //将当前节点的值替换成右节点的值
        this.value = this.right.value;
        //将当前节点的右节点设置成当前节点的右子树的右子树
        this.right = this.right.right;
        //将当前节点的左节点设置成新节点
        this.left = newNode;

    }

    /**
     * 右旋转实现平衡树
     */
    public void rightRotate() {
        Node node = new Node(this.value);
        node.right = this.right;
        node.left = this.left.right;
        this.value = this.left.value;
        this.left = this.left.left;
        this.right = node;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}